package b.h.y.bj.base.menudetail;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import b.h.y.bj.MainActivity;
import b.h.y.bj.R;
import b.h.y.bj.base.BaseMenuDetailPager;
import b.h.y.bj.base.TabDetailPager;
import b.h.y.bj.domain.NewsData;

/**
 * 菜单详情页-新闻
 * Created by YHB on 2016/1/4.
 */
public class NewsMenuDetailPager extends BaseMenuDetailPager implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;

    private List<TabDetailPager> mPagerList;

    private List<NewsData.NewsTabData> mNewsTabData; //页签的网络数据
    private MenuDetailAdapter menuDetailAdapter;
    private TabPageIndicator tabPageIndicator;

    public NewsMenuDetailPager(Activity activity, ArrayList<NewsData.NewsTabData> children) {
        super(activity);
        mNewsTabData = children;
    }

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.news_menu_detail, null);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_menu_detail);

        //初始化indicator
        tabPageIndicator =
                (TabPageIndicator) view.findViewById(R.id.indicator);

        ViewUtils.inject(this, view);


        return view;
    }

    @Override
    public void initData() {
        mPagerList = new ArrayList<>();

        for (int i = 0, len = mNewsTabData.size(); i < len; i++) {
            TabDetailPager tabDetailPager = new TabDetailPager(mActivity, mNewsTabData.get(i));
            mPagerList.add(tabDetailPager);
        }

        menuDetailAdapter = new MenuDetailAdapter();
        mViewPager.setAdapter(menuDetailAdapter);

        tabPageIndicator.setViewPager(mViewPager);

        //如果有pageIndicator 设置onPageChangeListener时,需要设置在indicator上
        //mViewPager.addOnPageChangeListener(this);
        tabPageIndicator.setOnPageChangeListener(this);
    }

    @OnClick(R.id.btn_next)
    public void nextPage(View view) {
        int current = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(++current);
    }

    class MenuDetailAdapter extends PagerAdapter {

        /**
         * 重写getPageTitle方法,用于viewPagerIndicator页签的显示
         *
         * @param position
         * @return
         */
        @Override
        public CharSequence getPageTitle(int position) {
            return mNewsTabData.get(position).title;
        }

        @Override
        public int getCount() {
            return mPagerList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TabDetailPager pager = mPagerList.get(position);
            container.addView(pager.mRootView);
            pager.initData();
            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        MainActivity mainActivity = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
        if (position == 0) { //只有在第一个页面,侧边栏才显示出来
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        } else {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
